#coding:utf-8
__author__ = 'hl'
from wx.grid import PyGridTableBase
import  wx
class BaseGridTable(PyGridTableBase):
    def __init__(self,datas,colLblsValue):
        PyGridTableBase.__init__(self)
        self.datas = datas
        self.colLblsValue = colLblsValue
        self.__setNumRowsAndNumCols()
        self.gridView = None

    #设置网格的行数和列数
    def __setNumRowsAndNumCols(self):
        #设置列数
        if not self.colLblsValue or not len(self.colLblsValue):
            print 'the value of colunms\' label is ether None or [] '
            self.numCol = 0
        else:
            self.numCol = len(self.colLblsValue)

        #设置行数
        if not self.datas or not len(self.datas):
            print 'the data of table is ether None or []'
            self.numRow = 0
        else:
            self.numRow = len(self.datas)


    def GetNumberRows(self):
        return self.numRow

    def GetNumberCols(self):
        return self.numCol

    def IsEmptyCell(self,row,col):
        try:
            if self.datas[row][col]:
                return False
            else:
                return True
        except:
            return True

    def GetValue(self,row,col):
        try:
            return self.datas[row][col];
        except:
            return ''
    def SetValue(self,row,col,value):
        try:
            self.datas[row][col] =value
        except:
            pass
        return self

    def GetColLabelValue(self,col):
        try:
            return self.colLblsValue[col]
        except:
            return 'Undefined'

    def GetRowLabelValue(self,row):
        try:
            return self.rowLblsValue[row]
        except:
            return 'Undefined'

    def SetRowLabelsValue(self,labelsValue):
        if type(labelsValue)==type([]):
            self.rowLblsValue=labelsValue
        return self

    def AppendRows(self,datas=None):
        if self.datas == None:
            self.datas=[]
        if datas:
            temp = self.numRow
            for rowData in datas:
                self.numRow += 1
                if type(rowData) != type([]):
                    self.datas.append(datas)
                    break
                else:
                    self.datas.append(rowData)

            if not self.gridView:
                self.gridView = self.GetView()
            self.gridView.BeginBatch()
            gridTableMessage = wx.grid.GridTableMessage(self,wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED,self.numRow - temp)
            self.gridView.ProcessTableMessage(gridTableMessage)
            self.gridView.EndBatch()



    def DeleteRows(self,pos,numRows):
        for row in range(numRows):
            del self.datas[0]
        self.numRow -= numRows
        if not self.gridView:
            self.gridView = self.GetView()
        self.gridView.BeginBatch()
        gridTableMessage = wx.grid.GridTableMessage(self,wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED,0,numRows)
        self.gridView.ProcessTableMessage(gridTableMessage)
        self.gridView.EndBatch()



    def ProcessTabmeMessage(self,messageType,pos = 0,num=1):
        if not self.gridView:
            self.gridView = self.GetView()
        self.gridView.BeginBatch()
        gridTableMessage = wx.grid.GridTableMessage(self,messageType,pos,num)
        self.gridView.ProcessTableMessage(gridTableMessage)
        self.gridView.EndBatch()
    #更新所有table中的数据,需要传入一个数据集合来替换旧的数据
    def UpdateTable(self,datas):
        #删除所有的数据
        self.DeleteRows(0,self.numRow)
        #添加新数据
        BaseGridTable.AppendRows(self,datas)
